//
//  PlayerSingle.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/7.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "PlayerSingle.h"
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>


@implementation PlayerSingle


- (void)dealloc {

   [_imageBackGroundView release];
   [_labelForTitle release];
   [_imageCoverLarge release];
    
    
  
    [_visualView release];
    [_player release];
    [_playerItem release];
    [_buttonPlayOrPause release];
    [_buttonRight release];
    [_buttonLeft release];
    [_progress release];
    [super dealloc];

}


+ (instancetype)sharePlayer {
    
    // 在静态区,只初始化一次 创建ItemVC3对象.
    static PlayerSingle *player = nil;
    static dispatch_once_t predicate;
    
    dispatch_once(&predicate, ^{
        player = [[PlayerSingle alloc]init];
    });
    
    return player;
}


- (instancetype)init {
    self = [super init];
    if (self) {
        

    }
    return self;
}

- (void)playWithUrl:(NSURL *)url showView:(UIView *)showView {

    
    self.imageBackGroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
    self.imageBackGroundView.frame = showView.bounds;
    [showView addSubview:self.imageBackGroundView];
    self.imageBackGroundView.userInteractionEnabled = YES;
    [self.imageBackGroundView release];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:2];
    self.visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
    self.visualView.frame = showView.bounds;
    [self.imageBackGroundView addSubview:self.visualView];
    [self.visualView release];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 24, 32, 32);
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [showView addSubview:backButton];
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *naozhongButton = [UIButton buttonWithType:UIButtonTypeCustom];
    naozhongButton.frame = CGRectMake(WIDTH - 40, 24, 32, 32);
    [naozhongButton setBackgroundImage:[UIImage imageNamed:@"naozhong"] forState:UIControlStateNormal];
    [showView addSubview:naozhongButton];
    
    self.labelForTitle = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, WIDTH - 100, 60)];
    [showView addSubview:self.labelForTitle];
    self.labelForTitle.text = @"主题";
    self.labelForTitle.font = [UIFont systemFontOfSize:20];
    self.labelForTitle.textColor = [UIColor whiteColor];
    self.labelForTitle.numberOfLines = 0;
    self.labelForTitle.textAlignment = NSTextAlignmentCenter;
    [self.labelForTitle release];
    
    self.imageCoverLarge = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
    self.imageCoverLarge.frame = CGRectMake((WIDTH - (HEIGHT - 60 - 300)) / 2, 110, HEIGHT - 60 - 300, HEIGHT - 60 - 300);
    [showView addSubview:self.imageCoverLarge];
    [self.imageCoverLarge release];
    
    
    self.progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.progress.frame = CGRectMake(50, HEIGHT - 200, [UIScreen mainScreen].bounds.size.width - 100, 30);
    self.progress.backgroundColor = [UIColor greenColor];
    [showView addSubview:self.progress];
    [self.progress release];
    
    
        self.buttonPlayOrPause = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttonPlayOrPause.frame = CGRectMake((WIDTH - 50) / 2, HEIGHT - 150, 50, 50);
        [showView addSubview:self.buttonPlayOrPause];
        [self.buttonPlayOrPause setBackgroundImage:[UIImage imageNamed:@"zanting2"] forState:UIControlStateNormal];
        [self.buttonPlayOrPause addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
        self.buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttonRight.frame = CGRectMake((WIDTH - 50) / 2 + 70, HEIGHT - 150, 50, 50);
        [showView addSubview:self.buttonRight];
        [self.buttonRight setBackgroundImage:[UIImage imageNamed:@"xiayiqu"] forState:UIControlStateNormal];
        [self.buttonRight addTarget:self action:@selector(handleButtonRight:) forControlEvents:UIControlEventTouchUpInside];
    
        self.buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        self.buttonLeft.frame = CGRectMake((WIDTH - 50) / 2 - 70, HEIGHT - 150, 50, 50);
        [showView addSubview:self.buttonLeft];
        [self.buttonLeft setBackgroundImage:[UIImage imageNamed:@"shangyiqu"] forState:UIControlStateNormal];
        [self.buttonLeft addTarget:self action:@selector(handleButtonLeft:) forControlEvents:UIControlEventTouchUpInside];

    
        self.playerItem = [AVPlayerItem playerItemWithURL:url];
        self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
        [self.player play];
    
        [self addProgressObserver];
//        [self addObserverToPlayerItem:self.playerItem];
    
}

// 返回按钮方法实现.
- (void)backButton:(UIButton *)button {
    
    NSNotification *notice = [NSNotification notificationWithName:@"返回" object:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
}

#pragma mark UIButton的点击方法.
// 播放按钮的点击事件
- (void)handleAction:(UIButton *)button {
    
    if (button.selected) {
        [self.buttonPlayOrPause setBackgroundImage:[UIImage imageNamed:@"zanting2"] forState:UIControlStateNormal];
        [self.player play];
    }else {
        [self.buttonPlayOrPause setBackgroundImage:[UIImage imageNamed:@"bofang2"] forState:UIControlStateNormal];
        [self.player pause];
    }
    button.selected = !button.selected;
}

- (void)handleButtonRight:(UIButton *)button {
    
    NSNotification *notice = [NSNotification notificationWithName:@"下一曲" object:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
}

- (void)handleButtonLeft:(UIButton *)button {
    
    NSNotification *notice = [NSNotification notificationWithName:@"上一曲" object:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
}




#pragma mark 监控

// 给播放器添加进度更新.
- (void)addProgressObserver {
    
    AVPlayerItem *playerItem = self.player.currentItem;
    UIProgressView *progress = self.progress;
    
    // 设置每秒执行一次.
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float current = CMTimeGetSeconds(time);
        float total = CMTimeGetSeconds([playerItem duration]);
        //        NSLog(@"当前已经播放了%.2fs",current);
        if (current) {
            [progress setProgress:(current/total) animated:YES];
        }
    }];
}

// 给AVPlayerItem添加监控.
- (void)addObserverToPlayerItem:(AVPlayerItem *)playerItem {
    
    // 监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放的状态.
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    // 监控网络加载情况的属性.
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}

// 移除AVPlayerItem上的监控.
- (void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem {
  
    [playerItem removeObserver:self forKeyPath:@"status"];
    [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
}

// 通过KVO监控播放器的状态. keyPath:监控属性, object:监视器, change:状态改变, context:上下文.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    AVPlayerItem *playerItem = object;
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
        if (status == AVPlayerStatusReadyToPlay) {
            NSLog(@"正在播放.....,视频总长度为:%.2f",CMTimeGetSeconds(playerItem.duration));
        }
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]) {
        NSArray *array = playerItem.loadedTimeRanges;
        // 本次缓冲范围.
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];
        
        float startSecends = CMTimeGetSeconds(timeRange.start);
        float durationSecends = CMTimeGetSeconds(timeRange.duration);
        // 缓冲总长度.
        NSTimeInterval totalBuffer = startSecends + durationSecends;
        NSLog(@"共缓冲:%.2f",totalBuffer);
    }
}


@end
